package com.edu.atams.controller;


import cn.hutool.core.util.StrUtil;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.atams.common.dto.ClassSubjectDto;


import com.edu.atams.common.dto.ListClassSubjectDto;
import com.edu.atams.common.lang.Result;
import com.edu.atams.entity.*;

import com.edu.atams.mapper.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author czj666
 * @since 2021-10-09
 */
@RestController
@RequestMapping("/class-subject")
public class ClassSubjectController extends BaseController {
    @Autowired
    ClassSubjectMapper classSubjectMapper;
    @Autowired
    SubjectMapper subjectMapper;
    @Autowired
    TeacherMapper teacherMapper;
    @Autowired
    ClassMapper classMapper;
    @Autowired
    GradeMapper gradeMapper;
    @GetMapping("/list")
    public Result list(String name,Integer classId, String schoolno) {
        //分页 根据subject_id,class_id,teacher_id查询
        Page<ClassSubject> page = classSubjectService.page(getPage(), new QueryWrapper<ClassSubject>()
                .in(StrUtil.isNotBlank(name), "subject_id", subjectService.list(new QueryWrapper<Subject>().
                        like("name", name)).stream().map(Subject::getId).collect(Collectors.toList()))
                .in(classId!=null, "class_id", classId)
                .in(StrUtil.isNotBlank(schoolno),"teacher_id",teacherService.list(new QueryWrapper<Teacher>().
                        like("schoolNo",schoolno)).stream().map(Teacher::getId).collect(Collectors.toList()))
        );
        //因为前端显示的不是id，需要显示根据id获取的其他字段，建立ClassSubjectDto，根据id将需要返回的字段保存在dto中进行返回
        List<ClassSubjectDto> classSubjectDtos=new ArrayList<>();
        for (ClassSubject record : page.getRecords()) {
            ClassSubjectDto classSubjectDto=new ClassSubjectDto();
            //回显时根据id进行回显
            classSubjectDto.setId(record.getId());
            if (record.getSubjectId()!=null){
                classSubjectDto.setSubjectName(subjectMapper.selectById(record.getSubjectId()).getName());
            }else {
                classSubjectDto.setSubjectName(null);
            }
            classSubjectDto.setClassName(gradeMapper.selectById(classMapper.selectById(record.getClassId()).getGradeId()).getYear()+
                    gradeMapper.selectById(classMapper.selectById(record.getClassId()).getGradeId()).getName()
                    +classMapper.selectById(record.getClassId()).getName());
            classSubjectDto.setClassId(record.getClassId());
            classSubjectDto.setTeacherId(teacherMapper.selectById(record.getTeacherId()).getId());
            classSubjectDto.setTeacherName(teacherMapper.selectById(record.getTeacherId()).getName());
            classSubjectDtos.add(classSubjectDto);
        }
        ListClassSubjectDto listClassSubjectDto = new ListClassSubjectDto();
        listClassSubjectDto.setRecords(classSubjectDtos);
        listClassSubjectDto.setSize(page.getSize());
        listClassSubjectDto.setCurrent(page.getCurrent());
        listClassSubjectDto.setTotal(page.getTotal());
        return Result.succ(listClassSubjectDto);
    }
    @PostMapping("/save")
    public Result save(@RequestBody ClassSubjectDto classSubjectDto){
        ClassSubject classSubject=new ClassSubject();
        classSubject.setSubjectId(subjectMapper.selectOne(new QueryWrapper<Subject>().eq("name",classSubjectDto.getSubjectName())).getId());
        classSubject.setClassId(classSubjectDto.getClassId());
        classSubject.setTeacherId(classSubjectDto.getTeacherId());
        return Result.succ(classSubjectService.save(classSubject));
    }
    @PostMapping("/del")
    public Result del(@RequestBody Integer[] ids){
        classSubjectService.removeByIds(Arrays.asList(ids));
        return Result.succ("");
    }
    @PostMapping("/update")
    public Result update(@RequestBody ClassSubjectDto classSubjectDto){
        ClassSubject classSubject=new ClassSubject();
        classSubject.setId(classSubjectDto.getId());
        classSubject.setSubjectId(subjectMapper.selectOne(new QueryWrapper<Subject>().eq("name",classSubjectDto.getSubjectName())).getId());
        classSubject.setClassId(classSubjectDto.getClassId());
        classSubject.setTeacherId(classSubjectDto.getTeacherId());
        return Result.succ(classSubjectService.updateById(classSubject));
    }
    //    @PostMapping("/upload")
//    public Result upload(@RequestParam("excel") MultipartFile excel) throws IOException {
//        return classSubjectService.excelUpload(excel);
//    }
    @GetMapping("/listSubjectName")
    public Result listSubjectName(){
        return Result.succ(subjectService.list());
    }
    @GetMapping("/listTeacher")
    public Result listTeacher(){
        return Result.succ(teacherService.list());
    }
}
